package dht.simulation;

public class VNode implements Comparable<VNode>, Cloneable {
	// [begin,end)
	public int begin;
	public int end;
	public double startTime;
	public int readCounter;
	public int writeCounter;
	public String macAddress;

	// public Vector<Double> readHistory;
	// public Vector<Double> writeHistory;

	public VNode(int begin) {
		this.begin = begin;
	}

	public VNode(int begin, int end, double startTime, String macAddress) {
		// readHistory = new Vector<Double>();
		// writeHistory = new Vector<Double>();
		this.begin = begin;
		this.end = end;
		this.startTime = startTime;
		this.macAddress = macAddress;
		this.readCounter = 0;
		this.writeCounter = 0;
	}

	public double getLoad(double currentTime) {
		return getReadLoad(currentTime) + getWriteLoad(currentTime);
	}

	public double getReadLoad(double currentTime) {
		if (readCounter <= 1 || currentTime <= startTime) {
			return 1;
		}
		 return GlobalVariables.readLoadWeight * readCounter
		 / (currentTime - startTime);
	}

	public double getWriteLoad(double currentTime) {
		if (writeCounter <= 1 || currentTime <= startTime) {
			return 1;
		}
		return GlobalVariables.writeLoadWeight * writeCounter
				/ (currentTime - startTime);
	}

	@Override
	public int compareTo(VNode o) {
		if (begin > o.begin)
			return 1;
		if (begin < o.begin)
			return -1;
		return 0;
	}

	public synchronized Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			throw new InternalError();
		}
	}

}
